const db = require('../dataBase/db');

//获取全部数据
const getList = (req, res) => {
    let sql = 'SELECT * FROM picture'
    db.query(sql, (err, data) => {
        if (err) {
            res.json(err);
        } else {
            res.json({msg:'查询成功！',data});
        }
    });
}

//添加数据
const add = (req, res) => {
    let sql = 'INSERT INTO picture (link) VALUES (?)'
    db.query(sql, [req.body.link],(err, data) => {
        if (err) {
            res.json(err);
        } else {
            res.json({ msg: '添加成功！', data });
        }
    });
}

//删除数据

const del = (req, res) => {
    let sql = 'DELETE FROM picture WHERE id = ?'
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            res.json(err);
        } else {
            res.json({ msg: '删除成功！', data });
        }
    });
}

//修改数据
const upd = (req, res) => {
    let sql = 'UPDATE picture SET link = ? where id = ?'
    db.query(sql, [req.body.link,req.body.id], (err, data) => {
        if (err) {
            res.json(err);
        } else {
            res.json({ msg: '修改成功！', data });
        }
    });
}

// 获取单条数据
const search = (req, res) => {
    let sql = 'SELECT * FROM picture WHERE id = ?'
    db.query(sql, [req.query.id], (err, data) => {
        if (err) {
            res.json(err);
        } else {
            res.json({ msg: '搜索成功！', data });
        }
    });
}
module.exports = {
    getList,
    add,
    del,
    upd,
    search,
}